package com.mty.mydemo.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.mty.mydemo.R;

import java.util.List;

/**
 * @ClassName:${class}
 * @Description： Created by ${mty} on 2016/3/15.
 * http://blog.csdn.net/yunyuliunian
 */
public class ExpandableAdapter extends BaseExpandableListAdapter {

    private Activity activity;
    private List<String> groupArray;
    private List<List<String>> childArray;

    public ExpandableAdapter(Activity activity, List<String> groupArray,
                             List<List<String>> childArray) {
        this.activity = activity;
        this.groupArray = groupArray;
        this.childArray = childArray;
    }

    @Override
    public int getGroupCount() {
        return groupArray.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childArray.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupArray.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childArray.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder=new ViewHolder();
            convertView = activity.getLayoutInflater().inflate(R.layout.item_expandable_group, null);
            viewHolder.itemGroupImg= (ImageView) convertView.findViewById(R.id.item_group_img);
            viewHolder.itemGroupTxt= (TextView) convertView.findViewById(R.id.item_group_txt);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.itemGroupTxt.setText(getGroup(groupPosition).toString());
        if (isExpanded) {
            viewHolder.itemGroupImg.setImageResource(R.drawable.icon_down_arrow);
        }else {
            viewHolder.itemGroupImg.setImageResource(R.drawable.icon_right_arrow);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             ViewGroup parent) {
        ChildViewHolder viewHolder;
        if (convertView == null) {
            viewHolder=new ChildViewHolder();
            convertView = activity.getLayoutInflater().inflate(R.layout.item_expandable_child, null);
            viewHolder.itemGroupTxt= (TextView) convertView.findViewById(R.id.item_child_txt);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ChildViewHolder) convertView.getTag();
        }
        viewHolder.itemGroupTxt.setText(getChild(groupPosition,childPosition).toString());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class ViewHolder {
        ImageView itemGroupImg;
        TextView itemGroupTxt;
    }

    class ChildViewHolder {
        TextView itemGroupTxt;
    }
}
